package com.cn.serverline.lineicp.productionLine.handler;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cn.serverline.lineicp.productionLine.entity.ProcessMaterials;
import com.cn.serverline.lineicp.workshopPlan.entity.SparePartsType;
import com.cn.serverline.lineicp.workshopPlan.mapper.SparePartsTypeMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * SparePartsHandler 类，实现了处理备件的具体操作
 *
 * @author Gmr
 * @since 2024/8/1
 */
@Component
public class SparePartsHandler implements MaterialHandler {

	@Resource
	private SparePartsTypeMapper sparePartsTypeMapper;

	@Override
	public void saveMaterial(ProcessMaterials processMaterials) {
		SparePartsType sparePartsType = new SparePartsType();
		BeanUtil.copyProperties(processMaterials, sparePartsType);
		sparePartsTypeMapper.insert(sparePartsType);
		processMaterials.setMaterialTypeId(sparePartsType.getId());
	}

	@Override
	public boolean validateMaterial(String code) {
		return sparePartsTypeMapper.selectCount(Wrappers.<SparePartsType>lambdaQuery()
				.eq(SparePartsType::getCode, code)) > 0;
	}

	@Override
	public Long getMaterialTypeId(String code) {
		return sparePartsTypeMapper.selectOne(Wrappers.<SparePartsType>lambdaQuery()
				.eq(SparePartsType::getCode, code)).getId();
	}
}
